 <!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 广告位管理</title>

    <meta name="keywords" content="{$Think.config.site.keywords}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">

</head>
<style>
#listtable .fa{
    cursor:pointer;
    color:#1ab394;
    margin-right:1em;
}

th .tag{font-size: 12px; color: #e66c6c; font-weight: normal;}
.ibox-title .tag{font-size: 12px; color: #e66c6c; font-weight: normal; display: inline-block; margin-left: 20px;}
</style>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
            <div class="col-sm-4">
                <div id="nestable-menu">
                	<button type="button" data-action="addnew" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> 新增</button>
                    <button type="button" data-action="save" class="btn btn-success btn-sm">保存</button>
                </div>
            </div>
      </div>
      <div class="row">
          <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>广告位列表</h5>
                
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                          	<div class="col-sm-12">
                                <div class="input-group pull-right">
                                <form id="myform" action="{:url('index')}" method="get" enctype="application/x-www-form-urlencoded">
                                <div class="input-group">
                                    <input type="text" placeholder="请输入关键词" class="input-sm form-control" value="{$Think.get.keywords ?? ''}" name="keywords">
                                    <input type="hidden" class="adp_id" name="adp_id" value="">
                                    <span class="input-group-btn">
                                        <button type="submit" class="btn btn-sm btn-primary"> 搜索</button> </span>
                                </div>
                                </form>
                                </div>
                            </div>
                            
                        </div>
                        <div class="table-responsive">
                            <table class="table table-striped" id="listtable">
                                <thead>
                                    <tr>

                                        <!-- <th width="30"><input name="checkall" id="checkall"  type="checkbox" class="i-checks" value="1"></th> -->
                                        <th width="40" class="id_sort" data-id_sort='{$sort}'>ID {if condition='$sort eq "adp_id desc"'}<span>&darr;</span>{else /}<span>&uarr;</span>{/if}</th>
                                        <th width="200">广告位名</th>
                                        <th width="200">广告位标识<span class="tag">（不能随意修改）</span></th>
                                        <th>说明</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                	{volist name="list" id="vo"}
                                    <tr data-id="{$vo.adp_id}" >
                                        <!-- <td>
                                            <input name="ids[]"  type="checkbox" class="i-checks" value="{$vo.adp_id}" {if ($vo.adp_id < 4)}disabled{/if}>
                                        </td> -->
                                        <td>{$vo.adp_id}</td>
                                        <td><input type="text"  class="form-control" value="{$vo.adp_name}"></td>
                                        <td><input type="text"  class="form-control" value="{$vo.adp_key}"></td>
                                        <td><input type="text"  class="form-control" value="{$vo.adp_desc}"></td>
                                      
                                        <td>
	                                        <a href="{:url('advlist',['adp_id'=>$vo.adp_id])}"><i class="fa fa-bars"> 广告列表</i></a>
                                            <!-- <i class="fa fa-plus"> 添加广告</i>  -->
                                            <a href="{:url('addadv',['adp_id'=>$vo.adp_id])}" class="fa fa-plus"> 添加广告</a>
	                                        <i class="fa fa-trash-o"> 删除</i>
                                        </td>
                                    </tr>
                                   {/volist}
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>
    <div id="modal-form" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                  <div class="row">
                    <div class="col-sm-12">
                            <h3 class="m-t-none m-b">新增广告位</h3>

                            <form id="editForm" role="form" class="form-horizontal">
                            <input type="hidden" name="status" value="1"/>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">广告位名称：</label>
                                    <div class="col-sm-8">
                                    	<input type="text" name="adp_name"  class="form-control" value="">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">广告位标识：</label>
                                    <div class="col-sm-8">
                                    	<input type="text" name="adp_key"  class="form-control" value="">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-12 control-label" style="text-align:left">说明：</label>
                                    <div class="col-sm-12">
                                    	<input type="text" name="adp_desc"  class="form-control">
                                    </div>
                                </div>
                                
                               
                                <div style="text-align:center;">
                                    <button class="btn btn-sm btn-primary m-t-n-xs" type="button" ><strong>保存</strong>
                                    </button>
                                    
                                    <button class="btn btn-sm btn-white  m-t-n-xs" type="button" data-dismiss="modal"><strong>取消</strong>
                                    </button>
                                </div>
                            </form>
                      </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/peity/jquery.peity.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/iCheck/icheck.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/majax.js"></script>
</body>

</html>
<script>
//切换排序
$('.id_sort').click(function(){
    var id_sort = $(this).data('id_sort');
    if(id_sort == "adp_id desc"){
        $('.adp_id').val("adp_id asc");
    }else{
        $('.adp_id').val("adp_id desc");
    }
    $('#myform').submit();
})

var layer_font='';
$(document).ready(function() {

	$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});

	//全选
	$("#checkall").bind("ifChecked",function(){
		$("input[name='ids[]']").iCheck("check");
	});
	//取消全选
	$("#checkall").bind("ifUnchecked",function(){
		$("input[name='ids[]']").iCheck("uncheck");
	});
	//顶部按钮
    $("#nestable-menu").on("click",
    function(e) {
        var target = $(e.target),
        action = target.data("action");
		//新增
        if (action === "addnew") {
            $('#modal-form').modal('show');
        }
		//保存排序
        if (action === "save") {
            var list = [];
			$("#listtable tbody tr").each(function(index, element) {
				$tr = $(this);
				var data = {
					adp_id : $tr.data("id"),
                    adp_name : $tr.find("td:eq(1) input").val(),
					adp_key : $tr.find("td:eq(2) input").val(),
					adp_desc : $tr.find("td:eq(3) input").val(),
				}
				list.push(data);
				
			});
			
            majax('{:url("editadp")}',window.JSON.stringify(list),function(d){
                if (d.code <= 0) {
                    layer.msg(d.msg, {icon: 2});
                }
            })
        }

    });
	
    
	//保存按钮注册
	$("#modal-form .btn-primary").bind("click",function(){
		var formData = $("#modal-form form").serialize();
		$('#modal-form').modal('hide');

		majax('{:url("addadp")}',formData,function(result){
			if (result.code>0) {
				layer.msg('新增成功！', {icon: 1});
				var $tr = $('<tr data-id="'+ result.data.adp_id +'" >\
                            <td>\
                            	 <input name="ids[]"  type="checkbox" class="i-checks" value="'+ result.data.adp_id +'">\
                            </td>\
                            <td>'+ result.data.adp_id +'</td>\
                            <td><input type="text"  class="form-control" value="'+ result.data.adp_name +'"></td>\
                            <td><input type="text"  class="form-control" value="'+ result.data.adp_key +'"></td>\
                            <td><input type="text"  class="form-control" value="'+ result.data.adp_desc +'"></td>\
                            <td><a href="{:url("advlist")}?adp_id='+result.data.adp_id+'"><i class="fa fa-bars"> 广告列表</i></a> <i class="fa fa-plus"> 添加广告</i> <i class="fa fa-trash-o"> 删除</i>\
                            </td>\
                        </tr>');
				$("#listtable tbody").append($tr);
				$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
			}else{
				layer.msg(result.msg, {icon: 5});
			}
		})
	})
	//取消按钮注册
	$("#modal-form .btn-white").bind("click",function(){
		$('#modal-form').modal('hide');
	})
	
	
	
	//添加广告
	// $("#listtable").on("click",".fa-plus",function(){
	// 	var $tr = $(this).closest("tr");
	// 	var id = $tr.data("id");
	// 	layer_font = layer.open({
	// 	  type: 2,
	// 	  title: '添加广告',
	// 	  shadeClose: true,
	// 	  shade: 0.8,
	// 	  area: ['900px', '90%'],
	// 	  content: '<?php echo \think\Request::instance()->root(); ?>/admin/Adv/addadv?adp_id='+id, //iframe的url
	//   	});
	// })

	//删除按钮注册
	$("#listtable").on("click",".fa-trash-o",function(){
		var $tr = $(this).closest("tr");
		var id = $tr.data("id");
		layer.confirm('删除广告位以及下面的广告？', {
			btn: ['确定删除','不删除'], //按钮
			shade: false //不显示遮罩
		}, function(){
			//确定删除
			majax('{:url("deladp")}',{ids:id},function(d){
				if (d.code>0) {
					layer.msg('删除成功', {icon: 1},function(){
						$tr.remove();
					});
					
				}else{
					layer.msg('删除失败', {icon: 5});
				}
			})
		},
		function(){
			
		});

		
	});

});	
function editSuccess(d){
    layer.close(layer_font);
    layer.msg(d.msg, {icon: 1});
}
</script>